package com.mrzhou.log;

import lombok.extern.slf4j.Slf4j;
import org.graylog2.syslog4j.SyslogConstants;
import org.graylog2.syslog4j.server.SyslogServer;
import org.graylog2.syslog4j.server.SyslogServerEventIF;
import org.graylog2.syslog4j.server.SyslogServerIF;
import org.graylog2.syslog4j.server.SyslogServerSessionlessEventHandlerIF;
import org.graylog2.syslog4j.server.impl.net.udp.UDPNetSyslogServerConfig;

import java.net.SocketAddress;

@Slf4j
public class SysLogServerMain {

    public static void main(String[] args) {
        SyslogServerIF serverInstance = SyslogServer.getInstance(SyslogConstants.UDP);
        UDPNetSyslogServerConfig serverConfig= (UDPNetSyslogServerConfig) serverInstance.getConfig();
        serverConfig.setHost("10.1.122.111");
        serverConfig.setPort(514);
        //防止数据过大被截取导致不完整
        serverConfig.setMaxMessageSize(SyslogConstants.SYSLOG_BUFFER_SIZE * 10 );
        serverConfig.addEventHandler(new SyslogServerSessionlessEventHandlerIF() {
            @Override
            public void event(SyslogServerIF syslogServerIF, SocketAddress socketAddress, SyslogServerEventIF syslogServerEventIF) {
                //syslog日志
                String message=syslogServerEventIF.getMessage();
                System.out.println(message);
                log.info("接收的日志：{}",message);
            }

            @Override
            public void exception(SyslogServerIF syslogServerIF, SocketAddress socketAddress, Exception e) {
                //异常处理
                e.printStackTrace();
            }

            @Override
            public void initialize(SyslogServerIF syslogServerIF) {
                //初始化处理
            }

            @Override
            public void destroy(SyslogServerIF syslogServerIF) {
                //摧毁
            }
        });
        serverInstance.run();
    }

}
